/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.editor;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.source.ISourceViewer;

import com.googlecode.twelfdevtools.model.ITwelfNamedElement;
import com.googlecode.twelfdevtools.model.TwelfModel;


/**
 * This class is loosely modeled after {@link org.eclipse.jface.text.DefaultTextHover}.
 * @author Kevin Bierhoff
 */
public class TwelfTypeHover implements ITextHover /*, ITextHoverExtension,
		ITextHoverExtension2 */{
	
	private final ISourceViewer sourceViewer;
	private final TwelfEditor editor;

	public TwelfTypeHover(TwelfEditor editor, ISourceViewer sourceViewer) {
		this.editor = editor;
		this.sourceViewer = sourceViewer;
	}

	public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		try {
			ITwelfNamedElement c = TwelfModel.getModel().getBinding(
					editor.getFile(), sourceViewer.getDocument(), hoverRegion);
			if(c != null)
				return c.getDeclaration();
		} 
		catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
		return TwelfModel.getModel().findTwelfWord(sourceViewer.getDocument(), offset);
	}

	// extension interfaces
//	public IInformationControlCreator getHoverControlCreator() {
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//	public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
//		// TODO Auto-generated method stub
//		return null;
//	}

}
